package com.qf.service.impl;

import com.qf.dao.GoodCarDao;
import com.qf.dao.GoodDao;
import com.qf.dao.impl.GoodCarDaoImpl;
import com.qf.dao.impl.GoodDaoImpl;
import com.qf.entity.Good;
import com.qf.entity.GoodCar;
import com.qf.service.GoodCarService;
import com.qf.util.DbUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2020/11/18 17:50
 * @Version 1.0
 */
public class GoodCarServiceImpl implements GoodCarService {
    private GoodCarDao dao=new GoodCarDaoImpl();
    private GoodDao goodDao=new GoodDaoImpl();
    @Override
    public int addCart(GoodCar car) {
        //我们应该先判断之前用户有没有添加过该商品到购物车
        //如果之前添加过，应该执行update
        //没有添加过就执行insert
        int result=0;
        try {
            DbUtils.begin();
            GoodCar oldCar=dao.selectGoodCarByUidAndGoodId(car);
            if (oldCar==null){
                //执行insert语句
                result=dao.insertGoodCar(car);
            }else{
                //执行修改操作
                oldCar.setGoodNum(oldCar.getGoodNum()+1);
                //新的小计应该是之前表中存在的加上传进来要添加的小计之和
                BigDecimal newGoodSum=oldCar.getGoodSum().add(car.getGoodSum());
                oldCar.setGoodSum(newGoodSum);
                result=dao.updateGoodNumAndGoodSumById(oldCar);

            }
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }


        return result;
    }

    @Override
    public List<GoodCar> getCarts(Integer uid) {
        List<GoodCar> list=null;
        try {
            DbUtils.begin();
            list=dao.selectGoodCarsByUid(uid);
            //由于前端界面需要的集合必须有商品相关的信息，这里插入商品的id
            for (GoodCar c:list){
                Good good=goodDao.selectGoodById(c.getGoodId());
                c.setGood(good);
            }
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }


        return list;
    }

    @Override
    public int updateCartNum(int goodId, BigDecimal goodPrice, int num, Integer uid) {
        int result=0;
        try {
            DbUtils.begin();
            GoodCar goodCar=new GoodCar();
            goodCar.setGoodId(goodId);
            goodCar.setUid(uid);
            GoodCar newGoodCar=dao.selectGoodCarByUidAndGoodId(goodCar);
            if (num==1){
                //执行增加一个的操作
                newGoodCar.setGoodNum(newGoodCar.getGoodNum()+num);
                newGoodCar.setGoodSum(newGoodCar.getGoodSum().add(goodPrice));
                result=dao.updateGoodNumAndGoodSumById(newGoodCar);

            }else if (num==-1){
                //执行减少一个的操作
                newGoodCar.setGoodNum(newGoodCar.getGoodNum()+num);
                newGoodCar.setGoodSum(newGoodCar.getGoodSum().subtract(goodPrice));
                result=dao.updateGoodNumAndGoodSumById(newGoodCar);
            }else if (num==0){
                //执行删除的操作
                result=dao.deleteGoodCarById(newGoodCar.getId());

            }
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }

        return result;
    }
//删除购物车
    @Override
    public int clearCart(int id) {
        int result=0;
        try {
            DbUtils.begin();
            result=dao.deleteGoodCarById(id);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }


        return result;
    }
    //清空购物车

    @Override
    public int clearAllCar(Integer uid) {
        int result=0;
        try {
            DbUtils.begin();
            result=dao.deleteGoodCarByUid(uid);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }


        return result;
    }
}
